<template>
  <div class="yunqi">

    <!--<header class="aui-bar aui-bar-nav">-->
    <!--<a class="aui-pull-left aui-btn">-->
    <!--<img src="../img/left.png" alt="">设置-->
    <!--</a>-->
    <!--</header>-->
    <div class="headerDiv"></div>
    <section>
      <div class="aui-content">
        <ul class="aui-list aui-list-in">
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-title">马桶静音</div>
              <div class="aui-list-item-right">
                <input id="Mute" @click="setMain('Mute')" v-model="Mute" type="checkbox" class="aui-switch" checked>
              </div>
            </div>
          </li>
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-title">自动翻盖</div>
              <div class="aui-list-item-right">
                <input id="fangG" type="checkbox" v-model="fangG" @click="setMain('fangG')" class="aui-switch" checked>
              </div>
            </div>
          </li>
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-title">手动清洗喷嘴</div>
              <div class="aui-list-item-right">
                <input id="qingXi" type="checkbox" v-model="qingXi" @click="setMain('qingXi')" class="aui-switch" checked>
              </div>
            </div>
          </li>
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-title">臀洗</div>
              <div class="aui-list-item-right">
                <input id="tunXi" type="checkbox" v-model="tunXi" @click="setMain('tunXi')" class="aui-switch" checked>
              </div>
            </div>
          </li>
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
              <div class="aui-list-item-title">喷嘴自洁</div>
            </div>
          </li>
          <!--<li class="aui-list-item" style="background-size:100% 0">-->
          <!--<div class="aui-list-item-inner">-->
          <!--<div class="aui-list-item-title">手动清洗检测槽</div>-->
          <!--<div class="aui-list-item-right">-->
          <!--<input type="checkbox" class="aui-switch" checked>-->
          <!--</div>-->
          <!--</div>-->
          <!--</li>-->
        </ul>
      </div>
      <div style="background: #f4f4f4;height:0.5rem;width:100%"></div>
      <div class="aui-content detection">
        <div class="aui-pull-left">检测更新</div>
        <div class="aui-pull-right"><i class="aui-iconfont aui-icon-right"></i></div>
      </div>
    </section>
    <!--<Alert v-if="stateEnd"  @closeTip="closeTip" :alertText="alertText"></Alert>-->
    <!--<Loading v-if="!connectEnd" ></Loading>-->
  </div>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
  import Loading from '../../common/Loading'
  import {Register} from '../../../store/_CRC'

  export default {
    name: 'SetMain',
    data () {
      return {
        OSTypeSet: false,
        Mute : true,
        fangG : true,
        qingXi: true,
        tunXi:true,
        alertText: null, //提示的内容
      }
    },
    components: {
      Loading
    },
    computed:{
      ...mapState([
        'reciveState',
        'reciveLen',
        'reciveData',
        'connectEnd',
        'stateData',
        'startAddr',
        'IsConnect ',
        'stateEnd',
        // 'OSType'
        // 'getRegisterBitValue',
        // 'setRegisterBitValue'
      ])
    },
    watch: {
      connectEnd (val) {
        // alert('读取数据')
        // this.WTGSTATE()
      },
      stateEnd(val){
        this.stateEnd = val
        // this.stateEnd = false
        // alert('change')
        this.initData()
      }

    },
    created(){
      this.INITmetodes()
      this.SetMainMUT()
      window.hilink.openBluetoothAdapter()
      sleep(1000)
      window.hilink.onBLEConnectionStateChange ("connetState")
      window.hilink.getSystemInfoSync('getSystemInfoSyncCallBack')

    },
    methods: {
      ...mapMutations([ 'WTGSTATE','TOTLECALLBACK','setRegisterBitValue','SetMainMUT'
      ]),
      INITmetodes(){
        var self = this
        window.sleep = function(numberMillis) {
          var now = new Date();
          var exitTime = now.getTime() + numberMillis;
          while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
              return;
          }
        }
        window.setNotify = function ()
        {
          var d = new Date()
          var n = d.getTime()
          var deviceId = window.getStorageSync('deviceId');
          var serviceId = window.getStorageSync('serviceId');
          var characteristicId = window.getStorageSync('characteristicId');
          window.hilink.notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId,true);
          // window.hilink.onBLECharacteristicValueChange ("ValueChange");
          console.log('执行成功:'+n);

          clearInterval(window.setIntervalId)
          // window.setIntervalId = setInterval("writeInterval()",2000)
          window.writeInterval()

          return true;
        }
        window.writeInterval = function()
        {
          var wrTore = Register.getF03String(1, 50, 30)
          var deviceId = window.getStorageSync('deviceId');
          var serviceId = window.getStorageSync('serviceId');
          var characteristicId = window.getStorageSync('characteristicId');
          var data = window.writeTo16(wrTore);
          console.log('写入：1，50，3')
          window.hilink.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, data,"writeCallback")
        }
        window.writeCallback = function () {

        }
        window.getSystemInfoSyncCallBack = function (re) {
          var info = eval("("+re+")")
          if(info.platform=='iOS'){
            self.OSTypeSet = true
          } else {
            self.OSTypeSet = false
          }
          var deviceId = window.getStorageSync('deviceId')
          window.hilink.createBLEConnection(deviceId)
        }
        window.connetState = function (re)
        {
          var d = new Date();
          var n = d.getTime();
          var data = eval("("+re+")")
          if(data.connected==true) {
            console.log('连接状态:'+data.connected+n)
            window.hilink.onBLECharacteristicValueChange ("ValueChange");
            setTimeout("setNotify()", 2000)
          } else {
            console.log('连接状态'+data.connected)
          }
        }
        window.writeTo16 = function (dataArr) {
          var newstr = '';
          for(var i = 0;i< dataArr.length;i++) {
            var str16 = (Array(2).join(0) + dataArr[i].toString(16)).slice(-2);
            newstr += str16
          }
          return newstr
        }
        window.readTo10 = function (dataStr) {
          var newArr = new Array
          for(var i = 0; i<dataStr.length;i=i+2) {
            newArr.push(parseInt(dataStr.substring(i,i+2),16))
          }
          return newArr
        }

        window.getStorageSync = function (key) {

          if (self.OSTypeSet==false) {
            var deId = "C8:FD:19:58:BC:10"
            var serviceId = "0000ffe0-0000-1000-8000-00805f9b34fb"
            var characteristicId = "0000ffe1-0000-1000-8000-00805f9b34fb"
          } else if(self.OSTypeSet==true){
            var deId = "ED14081B-52B5-544B-A211-45FAE38CE869"
            var serviceId = "ffe0"
            var characteristicId = "ffe1"
          }
          console.log(self.OSTypeSet)
          switch (key) {
            case 'deviceId':
              return deId
            case 'serviceId':
              return serviceId
            case 'characteristicId':
              return characteristicId
          }
        }

      },

      setMain (switchTo) {
        switch (switchTo) {
          case 'Mute':
            var Mute = window.document.getElementById("Mute");
            if (Mute.checked == true) {
              this.setRegisterBitValue({stateData:this.stateData,starRag:57,regBit:15,val:1})
            } else {
              this.setRegisterBitValue({stateData:this.stateData,starRag:57,regBit:15,val:0})
            }
            break
          case 'fangG':
            var fangG = window.document.getElementById("fangG");
            if (fangG.checked == true) {
              this.setRegisterBitValue({stateData:this.stateData,starRag:57,regBit:14,val:1})
            } else {
              this.setRegisterBitValue({stateData:this.stateData,starRag:57,regBit:14,val:0})
            }
            break
          case 'qingXi':
            var qingXi = window.document.getElementById("qingXi");
            if (qingXi.checked == true) {
              this.setRegisterBitValue({stateData:this.stateData,starRag:50,regBit:7,val:1})
            } else {
              this.setRegisterBitValue({stateData:this.stateData,starRag:50,regBit:7,val:0})
            }
            break
          case 'tunXi':
            var tunXi = window.document.getElementById("tunXi");
            if (tunXi.checked == true) {
              this.setRegisterBitValue({stateData:this.stateData,starRag:50,regBit:9,val:1})
            } else {
              this.setRegisterBitValue({stateData:this.stateData,starRag:50,regBit:9,val:0})
            }
            break
        }
      },
      initData(){
        console.log(this.stateData)
        this.Mute = Register.getRegisterBitValue(this.stateData[57-this.startAddr].data,57,15)

        this.fangG = Register.getRegisterBitValue(this.stateData[57-this.startAddr].data,57,14)

        this.qingXi = Register.getRegisterBitValue(this.stateData[50-this.startAddr].data,50,7)

        this.tunXi = Register.getRegisterBitValue(this.stateData[50-this.startAddr].data,50,9)

      }

    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  .headerDiv{
    height: 2rem;
    width: 100%;
    /*background-color: #288af0;*/
  }
  body{
    background: #ffffff !important;
  }
  .aui-bar-nav {
    background: #ffffff;
  }
  .aui-bar-nav a {
    color: #000;
  }
  .aui-content{
    margin:0 0.5rem;
  }
  .aui-content>ul>li{
    border-bottom: 1px solid #ccc;
    padding:0.5rem 0;
  }
  .detection{
    margin:0 1rem;
    height:2.25rem;
    line-height: 2.25rem;
    border-bottom:1px solid #ccc
  }
  .aui-list .aui-list-item-inner{
    padding:0.75rem 0.5rem 0.75rem 0;
  }
</style>
